<script setup>
import { ref, onMounted } from 'vue'
import searchNav from './components/searchNav.vue'
import orderItem from './components/orderItem.vue'
import { Nothing } from '@/components/common/index.js'
import { List } from 'vant'
import commonModel from '@/http/modules/common.js'

const page = ref(1)
const loading = ref(true)
const finished = ref(false)
const searchInfo = ref({})
const contractList = ref([])

function handleSearch(data) {
  searchInfo.value = data
  page.value = 1
  getList()
}

function handleReset() {
  page.value = 1
  getList()
}

function getList() {
  loading.value = true
  finished.value = false
  commonModel
    .gatewayApi({
      service: 'cb_trade_contract_page',
      bizContent: {
        pageNo: page.value,
        pageSize: 10,
        ...searchInfo.value,
        gmtCreateStart:searchInfo.value.gmtCreateStart ? `${searchInfo.value.gmtCreateStart} 00:00:00` : null,
        gmtCreateEnd:searchInfo.value.gmtCreateEnd ? `${searchInfo.value.gmtCreateEnd} 23:59:59` : null,
        contractStatusList: searchInfo.value.contractStatusList  && searchInfo.value.contractStatusList.length >0 ? searchInfo.value.contractStatusList[0].split(',') : [] 
      },
      version: '1.0.0',
    })
    .then((res) => {
      if (res) {
        if (page.value === 1) {
          contractList.value = res
        } else {
          contractList.value = contractList.value.concat(res)
        }
        if (res.length < 10) {
          finished.value = true
        }
        loading.value = false
      }
    })
}

function onLoad() {
  page.value++
  getList()
}

onMounted(() => {
  getList()
  // Toast.close()
})
</script>

<template>
  <search-nav @search="handleSearch" />
  <List
    v-model:loading="loading"
    :finished="finished"
    :finished-text="!contractList.length ? '' : '没有更多了'"
    @load="onLoad"
  >
    <order-item
      v-for="(item, index) in contractList"
      :key="index"
      :info="item"
      @reset="handleReset"
    />
    <Nothing v-if="!loading && !contractList.length" />
  </List>
</template>

<style lang="scss" scoped>
.van-list {
  padding: 0 0.12rem 0.1rem;
}
</style>
